library SatchelCharge initializer init requires LegacySystem


	private function CollapsedRocks takes nothing returns boolean
		return GetUnitTypeId(GetFilterUnit())=='e00J'
	endfunction

	private function ThermalDetonatorConditions takes nothing returns boolean
		local integer i = GetSpellAbilityId()
		return i=='A05Q' or i=='A05R'
	endfunction

	private function satchelDamageLoop takes nothing returns nothing
	   local timer t = GetExpiredTimer() 
	   local unit source = getUnit(t,"source")
	   local real x = getReal(t,"x")
	   local real y = getReal(t,"y")
	   local integer dmg = getInteger(t,"damage_per_tick")
	   local integer left = getInteger(t,"ticks_left")
	   set left = left -1
	   if( left<0 ) then 
		  set t = null
		  set source = null
		  call FlushHandleLocals(t)
		  call ReleaseTimer(t)
		  return
	   endif
	   
	   call putInteger(t,"ticks_left", left)
	   call DamageAreaExplosion(source,x,y,dmg,400.00)
	   set t = null
	   set source = null
	endfunction


	private function satchelDamage takes real x, real y, unit u returns nothing
		local timer t = NewTimer()
		local rect r 
		local group g = CreateGroup()
		call putReal(t,"x",x)
		call putReal(t,"y",y)
		call putInteger(t,"damage_per_tick", 1000)
		call putInteger(t,"ticks_left", 11)
		call putUnit(t,"source",u)
			
		set r = Rect(x-340,y-340,x+340,y+340)
		call DamageTrees(r,10000)
			
		call SetRect(r,x-340.0,y-340.0, x+340.0, y+340.0)
		call GroupEnumUnitsInRect(g, r, Condition(function killexplosiveFast))
		
		call DamageAreaExplosion(u,x,y, 6000,400.00)
		call TimerStart(t,0.05,false, function satchelDamageLoop)
		
		call DestroyGroup(g)
		call RemoveRect(r)
		set t = null
		set r = null
		set g = null
	endfunction



	private function satchelKaboom takes player p, real x, real y returns nothing
		local effect f 
		local real offset = 128.0+16.00  
		local unit u 
		
		set u = CreateUnit(p,'e00A',x,y,0.0)
		call UnitApplyTimedLife(u,'Bhwd',1.5)
			
		set u = CreateUnit(p,'e004',x,y,0.0)
		call UnitApplyTimedLife(u,'Bhwd',9.0)
			
		set f = AddSpecialEffect( "Effect_FireRing.mdx",x,y)
		call DestroyEffect(f)
		set f = AddSpecialEffect("Objects\\Spawnmodels\\Other\\NeutralBuildingExplosion\\NeutralBuildingExplosion.mdl"  ,x+offset,y+offset )
		call DestroyEffect(f)
		set f = AddSpecialEffect("Objects\\Spawnmodels\\Other\\NeutralBuildingExplosion\\NeutralBuildingExplosion.mdl" , x-offset,y-offset)
		call DestroyEffect(f) 
		set f = AddSpecialEffect("Objects\\Spawnmodels\\Other\\NeutralBuildingExplosion\\NeutralBuildingExplosion.mdl"  ,x+offset,y-offset )
		call DestroyEffect(f)      
		set f = AddSpecialEffect("Objects\\Spawnmodels\\Other\\NeutralBuildingExplosion\\NeutralBuildingExplosion.mdl"  ,x-offset,y+offset )
		call DestroyEffect(f)  
		set f = AddSpecialEffect("Objects\\Spawnmodels\\Undead\\ImpaleTargetDust\\ImpaleTargetDust.mdl",x,y)
		call DestroyEffect(f)
		
		set u = null
		set f = null
	endfunction 

	private function ThermalDetonator takes nothing returns nothing
		local integer cave = 0
		local unit u = GetTriggerUnit()
		local unit u2
		local location l = GetUnitLoc(u)
		local real x = GetUnitX(u)
		local real y = GetUnitY(u)
		local player p = GetOwningPlayer(u)
		local group g
		local group g2
		local effect f 
		local rect r 

		if udg_Mode!=3 then
			call PingMinimapEx(GetLocationX(l), GetLocationY(l), 5.0, 100, 196, 80, false)
			call ALICEText(5.0, "Satchel charge in place. Initiating countdown.." )
		endif
		set u2 = CreateUnit(Player(14),'n00P',x,y,0.0) //DangerZone Dummy
		call UnitApplyTimedLife(u2,'Bhwd',12.0)

		call PolledWait(udg_Primer)

		if udg_Intermission then
			call PolledWait(8.5)
		endif
		
		call SetSoundPosition(gg_snd_ThermalReady,x,y,0)
		call SetSoundVolume(gg_snd_ThermalReady,127)
		call StartSound(gg_snd_ThermalReady)
		
		
		call PolledWait(1.75)
		

		set g2 = GetUnitsOfPlayerAndTypeId(p,'n000')
		if GetWidgetLife(FirstOfGroup(g2))<0.405 or udg_SectorClear == true then
			set u2 = null
			call DestroyGroup(g2)
			set g2 = null
			set u = null
			call TriggerSleepAction(5.0)
			call RemoveLocation(l)
			set l = null
			return
		endif

		call satchelKaboom(p, x,y)
		call satchelDamage(x,y,u)
		call EarShockArea(x,y,800)
			
			
		call TriggerSleepAction(1.5)
		

		set g = AMGetUnitsInRange(800, l, Condition(function CollapsedRocks))
		if ( (RectContainsCoords(gg_rct_SpawnEastCave,x,y) or RectContainsCoords(gg_rct_SpawnWestCave,x,y) or RectContainsCoords(gg_rct_SpawnSouthCave,x,y) or RectContainsCoords(gg_rct_SpawnRiverCave,x,y) or RectContainsCoords(gg_rct_SpawnTownEastCave,x,y) or RectContainsCoords(gg_rct_SpawnTownWestCave,x,y)) and FirstOfGroup(g)==null) then
			set cave = 1
		endif
		if cave==1 then
			call SetSoundPosition(gg_snd_CaveCollapse,x,y,0)
			call SetSoundVolume(gg_snd_CaveCollapse, 127)
			call StartSound(gg_snd_CaveCollapse)
			call DestroyEffect(AddSpecialEffect("Objects\\Spawnmodels\\Undead\\ImpaleTargetDust\\ImpaleTargetDust.mdl",x,y))
			call CreateUnit(Player(13),'e00J',x,y,0.0) //Collapsed Rocks
			call PolledWait(2.5)
			call SetPlayerHandicap(Player(13),GetPlayerHandicap(Player(13))-0.02)
			call SetPlayerHandicap(Player(14),GetPlayerHandicap(Player(14))-0.02)
			set udg_MissionCave = udg_MissionCave + 1
			if udg_MissionCave==6 and udg_Mode==1 then
				call ALICE2Text(10.0, "All Caves have been successfully sealed. |cff1e90ff100 XP|r awarded to all Marines." )
				set udg_MissionsCompleted=udg_MissionsCompleted+1
				loop
					exitwhen cave>udg_InitialPlayers
					if GetPlayerSlotState(ConvertedPlayer(udg_Sorted[cave]))!=PLAYER_SLOT_STATE_LEFT then
						set udg_Experience[udg_Sorted[cave]] = udg_Experience[udg_Sorted[cave]] + 100
						if GetUnitTypeId(udg_Hero[udg_Sorted[cave]])=='H006' then
							call SetHeroLevel(udg_Hero[udg_Sorted[cave]],GetHeroLevel(udg_Hero[udg_Sorted[cave]])+1,true)
						endif
					endif
					set cave=cave+1
				endloop
			else
				if udg_MissionCave==5 then
					call DisplayTimedTextToPlayer( GetLocalPlayer(), 0.0, 0.0, 5.0, "A cave entrance has been sealed. 1 active cave remaining." )
				elseif udg_MissionCave<5 then
					call DisplayTimedTextToPlayer( GetLocalPlayer(), 0.0, 0.0, 5.0, "A cave entrance has been sealed. " + I2S(6-udg_MissionCave) + " active caves remaining." )
				endif
			endif
		endif

		call DestroyGroup(g)
		call DestroyGroup(g2)
		set g = null
		set g2 = null
		set u = null
		set u2 = null
		call RemoveLocation(l)
		set l = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT  )
		call TriggerAddCondition( t, Condition( function ThermalDetonatorConditions ) )
		call TriggerAddAction(t,function ThermalDetonator)
	endfunction

endlibrary